{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数据读取及基本处理\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "#查看数据分布是否对称/计算斜度\n",
    "from scipy.stats import skew\n",
    "\n",
    "#可视化\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "from IPython.display import display\n",
    "# Definitions\n",
    "pd.set_option('display.float_format', lambda x: '%.3f' % x)\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Id</th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>MSZoning</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>LandContour</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>...</th>\n",
       "      <th>PoolArea</th>\n",
       "      <th>PoolQC</th>\n",
       "      <th>Fence</th>\n",
       "      <th>MiscFeature</th>\n",
       "      <th>MiscVal</th>\n",
       "      <th>MoSold</th>\n",
       "      <th>YrSold</th>\n",
       "      <th>SaleType</th>\n",
       "      <th>SaleCondition</th>\n",
       "      <th>SalePrice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>65.000</td>\n",
       "      <td>8450</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>208500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>RL</td>\n",
       "      <td>80.000</td>\n",
       "      <td>9600</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>2007</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>181500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>68.000</td>\n",
       "      <td>11250</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>223500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>70</td>\n",
       "      <td>RL</td>\n",
       "      <td>60.000</td>\n",
       "      <td>9550</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2006</td>\n",
       "      <td>WD</td>\n",
       "      <td>Abnorml</td>\n",
       "      <td>140000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>84.000</td>\n",
       "      <td>14260</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>250000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 81 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Id  MSSubClass MSZoning  LotFrontage  LotArea Street Alley LotShape  \\\n",
       "0   1          60       RL       65.000     8450   Pave   NaN      Reg   \n",
       "1   2          20       RL       80.000     9600   Pave   NaN      Reg   \n",
       "2   3          60       RL       68.000    11250   Pave   NaN      IR1   \n",
       "3   4          70       RL       60.000     9550   Pave   NaN      IR1   \n",
       "4   5          60       RL       84.000    14260   Pave   NaN      IR1   \n",
       "\n",
       "  LandContour Utilities    ...     PoolArea PoolQC Fence MiscFeature MiscVal  \\\n",
       "0         Lvl    AllPub    ...            0    NaN   NaN         NaN       0   \n",
       "1         Lvl    AllPub    ...            0    NaN   NaN         NaN       0   \n",
       "2         Lvl    AllPub    ...            0    NaN   NaN         NaN       0   \n",
       "3         Lvl    AllPub    ...            0    NaN   NaN         NaN       0   \n",
       "4         Lvl    AllPub    ...            0    NaN   NaN         NaN       0   \n",
       "\n",
       "  MoSold YrSold  SaleType  SaleCondition  SalePrice  \n",
       "0      2   2008        WD         Normal     208500  \n",
       "1      5   2007        WD         Normal     181500  \n",
       "2      9   2008        WD         Normal     223500  \n",
       "3      2   2006        WD        Abnorml     140000  \n",
       "4     12   2008        WD         Normal     250000  \n",
       "\n",
       "[5 rows x 81 columns]"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入数据\n",
    "train = pd.read_csv('Ames_House_train.csv')\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Id</th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>MSZoning</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>LandContour</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>...</th>\n",
       "      <th>ScreenPorch</th>\n",
       "      <th>PoolArea</th>\n",
       "      <th>PoolQC</th>\n",
       "      <th>Fence</th>\n",
       "      <th>MiscFeature</th>\n",
       "      <th>MiscVal</th>\n",
       "      <th>MoSold</th>\n",
       "      <th>YrSold</th>\n",
       "      <th>SaleType</th>\n",
       "      <th>SaleCondition</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1461</td>\n",
       "      <td>20</td>\n",
       "      <td>RH</td>\n",
       "      <td>80.000</td>\n",
       "      <td>11622</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>...</td>\n",
       "      <td>120</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MnPrv</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1462</td>\n",
       "      <td>20</td>\n",
       "      <td>RL</td>\n",
       "      <td>81.000</td>\n",
       "      <td>14267</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Gar2</td>\n",
       "      <td>12500</td>\n",
       "      <td>6</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1463</td>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>74.000</td>\n",
       "      <td>13830</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MnPrv</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1464</td>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>78.000</td>\n",
       "      <td>9978</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1465</td>\n",
       "      <td>120</td>\n",
       "      <td>RL</td>\n",
       "      <td>43.000</td>\n",
       "      <td>5005</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>HLS</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>...</td>\n",
       "      <td>144</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 80 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Id  MSSubClass MSZoning  LotFrontage  LotArea Street Alley LotShape  \\\n",
       "0  1461          20       RH       80.000    11622   Pave   NaN      Reg   \n",
       "1  1462          20       RL       81.000    14267   Pave   NaN      IR1   \n",
       "2  1463          60       RL       74.000    13830   Pave   NaN      IR1   \n",
       "3  1464          60       RL       78.000     9978   Pave   NaN      IR1   \n",
       "4  1465         120       RL       43.000     5005   Pave   NaN      IR1   \n",
       "\n",
       "  LandContour Utilities      ...       ScreenPorch PoolArea PoolQC  Fence  \\\n",
       "0         Lvl    AllPub      ...               120        0    NaN  MnPrv   \n",
       "1         Lvl    AllPub      ...                 0        0    NaN    NaN   \n",
       "2         Lvl    AllPub      ...                 0        0    NaN  MnPrv   \n",
       "3         Lvl    AllPub      ...                 0        0    NaN    NaN   \n",
       "4         HLS    AllPub      ...               144        0    NaN    NaN   \n",
       "\n",
       "  MiscFeature MiscVal MoSold  YrSold  SaleType  SaleCondition  \n",
       "0         NaN       0      6    2010        WD         Normal  \n",
       "1        Gar2   12500      6    2010        WD         Normal  \n",
       "2         NaN       0      3    2010        WD         Normal  \n",
       "3         NaN       0      6    2010        WD         Normal  \n",
       "4         NaN       0      1    2010        WD         Normal  \n",
       "\n",
       "[5 rows x 80 columns]"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = pd.read_csv(\"Ames_House_test.csv\")\n",
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "MSZoning属性的不同取值和出现的次数\n",
      "RL         1151\n",
      "RM          218\n",
      "FV           65\n",
      "RH           16\n",
      "C (all)      10\n",
      "Name: MSZoning, dtype: int64\n",
      "\n",
      "Street属性的不同取值和出现的次数\n",
      "Pave    1454\n",
      "Grvl       6\n",
      "Name: Street, dtype: int64\n",
      "\n",
      "Alley属性的不同取值和出现的次数\n",
      "Grvl    50\n",
      "Pave    41\n",
      "Name: Alley, dtype: int64\n",
      "\n",
      "LotShape属性的不同取值和出现的次数\n",
      "Reg    925\n",
      "IR1    484\n",
      "IR2     41\n",
      "IR3     10\n",
      "Name: LotShape, dtype: int64\n",
      "\n",
      "LandContour属性的不同取值和出现的次数\n",
      "Lvl    1311\n",
      "Bnk      63\n",
      "HLS      50\n",
      "Low      36\n",
      "Name: LandContour, dtype: int64\n",
      "\n",
      "Utilities属性的不同取值和出现的次数\n",
      "AllPub    1459\n",
      "NoSeWa       1\n",
      "Name: Utilities, dtype: int64\n",
      "\n",
      "LotConfig属性的不同取值和出现的次数\n",
      "Inside     1052\n",
      "Corner      263\n",
      "CulDSac      94\n",
      "FR2          47\n",
      "FR3           4\n",
      "Name: LotConfig, dtype: int64\n",
      "\n",
      "LandSlope属性的不同取值和出现的次数\n",
      "Gtl    1382\n",
      "Mod      65\n",
      "Sev      13\n",
      "Name: LandSlope, dtype: int64\n",
      "\n",
      "Neighborhood属性的不同取值和出现的次数\n",
      "NAmes      225\n",
      "CollgCr    150\n",
      "OldTown    113\n",
      "Edwards    100\n",
      "Somerst     86\n",
      "Gilbert     79\n",
      "NridgHt     77\n",
      "Sawyer      74\n",
      "NWAmes      73\n",
      "SawyerW     59\n",
      "BrkSide     58\n",
      "Crawfor     51\n",
      "Mitchel     49\n",
      "NoRidge     41\n",
      "Timber      38\n",
      "IDOTRR      37\n",
      "ClearCr     28\n",
      "SWISU       25\n",
      "StoneBr     25\n",
      "MeadowV     17\n",
      "Blmngtn     17\n",
      "BrDale      16\n",
      "Veenker     11\n",
      "NPkVill      9\n",
      "Blueste      2\n",
      "Name: Neighborhood, dtype: int64\n",
      "\n",
      "Condition1属性的不同取值和出现的次数\n",
      "Norm      1260\n",
      "Feedr       81\n",
      "Artery      48\n",
      "RRAn        26\n",
      "PosN        19\n",
      "RRAe        11\n",
      "PosA         8\n",
      "RRNn         5\n",
      "RRNe         2\n",
      "Name: Condition1, dtype: int64\n",
      "\n",
      "Condition2属性的不同取值和出现的次数\n",
      "Norm      1445\n",
      "Feedr        6\n",
      "RRNn         2\n",
      "Artery       2\n",
      "PosN         2\n",
      "PosA         1\n",
      "RRAn         1\n",
      "RRAe         1\n",
      "Name: Condition2, dtype: int64\n",
      "\n",
      "BldgType属性的不同取值和出现的次数\n",
      "1Fam      1220\n",
      "TwnhsE     114\n",
      "Duplex      52\n",
      "Twnhs       43\n",
      "2fmCon      31\n",
      "Name: BldgType, dtype: int64\n",
      "\n",
      "HouseStyle属性的不同取值和出现的次数\n",
      "1Story    726\n",
      "2Story    445\n",
      "1.5Fin    154\n",
      "SLvl       65\n",
      "SFoyer     37\n",
      "1.5Unf     14\n",
      "2.5Unf     11\n",
      "2.5Fin      8\n",
      "Name: HouseStyle, dtype: int64\n",
      "\n",
      "RoofStyle属性的不同取值和出现的次数\n",
      "Gable      1141\n",
      "Hip         286\n",
      "Flat         13\n",
      "Gambrel      11\n",
      "Mansard       7\n",
      "Shed          2\n",
      "Name: RoofStyle, dtype: int64\n",
      "\n",
      "RoofMatl属性的不同取值和出现的次数\n",
      "CompShg    1434\n",
      "Tar&Grv      11\n",
      "WdShngl       6\n",
      "WdShake       5\n",
      "ClyTile       1\n",
      "Roll          1\n",
      "Membran       1\n",
      "Metal         1\n",
      "Name: RoofMatl, dtype: int64\n",
      "\n",
      "Exterior1st属性的不同取值和出现的次数\n",
      "VinylSd    515\n",
      "HdBoard    222\n",
      "MetalSd    220\n",
      "Wd Sdng    206\n",
      "Plywood    108\n",
      "CemntBd     61\n",
      "BrkFace     50\n",
      "WdShing     26\n",
      "Stucco      25\n",
      "AsbShng     20\n",
      "BrkComm      2\n",
      "Stone        2\n",
      "AsphShn      1\n",
      "CBlock       1\n",
      "ImStucc      1\n",
      "Name: Exterior1st, dtype: int64\n",
      "\n",
      "Exterior2nd属性的不同取值和出现的次数\n",
      "VinylSd    504\n",
      "MetalSd    214\n",
      "HdBoard    207\n",
      "Wd Sdng    197\n",
      "Plywood    142\n",
      "CmentBd     60\n",
      "Wd Shng     38\n",
      "Stucco      26\n",
      "BrkFace     25\n",
      "AsbShng     20\n",
      "ImStucc     10\n",
      "Brk Cmn      7\n",
      "Stone        5\n",
      "AsphShn      3\n",
      "CBlock       1\n",
      "Other        1\n",
      "Name: Exterior2nd, dtype: int64\n",
      "\n",
      "MasVnrType属性的不同取值和出现的次数\n",
      "None       864\n",
      "BrkFace    445\n",
      "Stone      128\n",
      "BrkCmn      15\n",
      "Name: MasVnrType, dtype: int64\n",
      "\n",
      "ExterQual属性的不同取值和出现的次数\n",
      "TA    906\n",
      "Gd    488\n",
      "Ex     52\n",
      "Fa     14\n",
      "Name: ExterQual, dtype: int64\n",
      "\n",
      "ExterCond属性的不同取值和出现的次数\n",
      "TA    1282\n",
      "Gd     146\n",
      "Fa      28\n",
      "Ex       3\n",
      "Po       1\n",
      "Name: ExterCond, dtype: int64\n",
      "\n",
      "Foundation属性的不同取值和出现的次数\n",
      "PConc     647\n",
      "CBlock    634\n",
      "BrkTil    146\n",
      "Slab       24\n",
      "Stone       6\n",
      "Wood        3\n",
      "Name: Foundation, dtype: int64\n",
      "\n",
      "BsmtQual属性的不同取值和出现的次数\n",
      "TA    649\n",
      "Gd    618\n",
      "Ex    121\n",
      "Fa     35\n",
      "Name: BsmtQual, dtype: int64\n",
      "\n",
      "BsmtCond属性的不同取值和出现的次数\n",
      "TA    1311\n",
      "Gd      65\n",
      "Fa      45\n",
      "Po       2\n",
      "Name: BsmtCond, dtype: int64\n",
      "\n",
      "BsmtExposure属性的不同取值和出现的次数\n",
      "No    953\n",
      "Av    221\n",
      "Gd    134\n",
      "Mn    114\n",
      "Name: BsmtExposure, dtype: int64\n",
      "\n",
      "BsmtFinType1属性的不同取值和出现的次数\n",
      "Unf    430\n",
      "GLQ    418\n",
      "ALQ    220\n",
      "BLQ    148\n",
      "Rec    133\n",
      "LwQ     74\n",
      "Name: BsmtFinType1, dtype: int64\n",
      "\n",
      "BsmtFinType2属性的不同取值和出现的次数\n",
      "Unf    1256\n",
      "Rec      54\n",
      "LwQ      46\n",
      "BLQ      33\n",
      "ALQ      19\n",
      "GLQ      14\n",
      "Name: BsmtFinType2, dtype: int64\n",
      "\n",
      "Heating属性的不同取值和出现的次数\n",
      "GasA     1428\n",
      "GasW       18\n",
      "Grav        7\n",
      "Wall        4\n",
      "OthW        2\n",
      "Floor       1\n",
      "Name: Heating, dtype: int64\n",
      "\n",
      "HeatingQC属性的不同取值和出现的次数\n",
      "Ex    741\n",
      "TA    428\n",
      "Gd    241\n",
      "Fa     49\n",
      "Po      1\n",
      "Name: HeatingQC, dtype: int64\n",
      "\n",
      "CentralAir属性的不同取值和出现的次数\n",
      "Y    1365\n",
      "N      95\n",
      "Name: CentralAir, dtype: int64\n",
      "\n",
      "Electrical属性的不同取值和出现的次数\n",
      "SBrkr    1334\n",
      "FuseA      94\n",
      "FuseF      27\n",
      "FuseP       3\n",
      "Mix         1\n",
      "Name: Electrical, dtype: int64\n",
      "\n",
      "KitchenQual属性的不同取值和出现的次数\n",
      "TA    735\n",
      "Gd    586\n",
      "Ex    100\n",
      "Fa     39\n",
      "Name: KitchenQual, dtype: int64\n",
      "\n",
      "Functional属性的不同取值和出现的次数\n",
      "Typ     1360\n",
      "Min2      34\n",
      "Min1      31\n",
      "Mod       15\n",
      "Maj1      14\n",
      "Maj2       5\n",
      "Sev        1\n",
      "Name: Functional, dtype: int64\n",
      "\n",
      "FireplaceQu属性的不同取值和出现的次数\n",
      "Gd    380\n",
      "TA    313\n",
      "Fa     33\n",
      "Ex     24\n",
      "Po     20\n",
      "Name: FireplaceQu, dtype: int64\n",
      "\n",
      "GarageType属性的不同取值和出现的次数\n",
      "Attchd     870\n",
      "Detchd     387\n",
      "BuiltIn     88\n",
      "Basment     19\n",
      "CarPort      9\n",
      "2Types       6\n",
      "Name: GarageType, dtype: int64\n",
      "\n",
      "GarageFinish属性的不同取值和出现的次数\n",
      "Unf    605\n",
      "RFn    422\n",
      "Fin    352\n",
      "Name: GarageFinish, dtype: int64\n",
      "\n",
      "GarageQual属性的不同取值和出现的次数\n",
      "TA    1311\n",
      "Fa      48\n",
      "Gd      14\n",
      "Ex       3\n",
      "Po       3\n",
      "Name: GarageQual, dtype: int64\n",
      "\n",
      "GarageCond属性的不同取值和出现的次数\n",
      "TA    1326\n",
      "Fa      35\n",
      "Gd       9\n",
      "Po       7\n",
      "Ex       2\n",
      "Name: GarageCond, dtype: int64\n",
      "\n",
      "PavedDrive属性的不同取值和出现的次数\n",
      "Y    1340\n",
      "N      90\n",
      "P      30\n",
      "Name: PavedDrive, dtype: int64\n",
      "\n",
      "PoolQC属性的不同取值和出现的次数\n",
      "Gd    3\n",
      "Ex    2\n",
      "Fa    2\n",
      "Name: PoolQC, dtype: int64\n",
      "\n",
      "Fence属性的不同取值和出现的次数\n",
      "MnPrv    157\n",
      "GdPrv     59\n",
      "GdWo      54\n",
      "MnWw      11\n",
      "Name: Fence, dtype: int64\n",
      "\n",
      "MiscFeature属性的不同取值和出现的次数\n",
      "Shed    49\n",
      "Othr     2\n",
      "Gar2     2\n",
      "TenC     1\n",
      "Name: MiscFeature, dtype: int64\n",
      "\n",
      "SaleType属性的不同取值和出现的次数\n",
      "WD       1267\n",
      "New       122\n",
      "COD        43\n",
      "ConLD       9\n",
      "ConLI       5\n",
      "ConLw       5\n",
      "CWD         4\n",
      "Oth         3\n",
      "Con         2\n",
      "Name: SaleType, dtype: int64\n",
      "\n",
      "SaleCondition属性的不同取值和出现的次数\n",
      "Normal     1198\n",
      "Partial     125\n",
      "Abnorml     101\n",
      "Family       20\n",
      "Alloca       12\n",
      "AdjLand       4\n",
      "Name: SaleCondition, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "#对类别型特征，观察其取值范围及直方图\n",
    "categorical_features = train.select_dtypes(include = [\"object\"]).columns\n",
    "for col in categorical_features:\n",
    "    print('\\n%s属性的不同取值和出现的次数'%col)\n",
    "    print(train[col].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train.drop(['Id'], inplace = True, axis = 1)\n",
    "test_id = test['Id']\n",
    "test.drop(['Id'], inplace = True, axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZsAAAEWCAYAAACwtjr+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3X+8XFV57/HPNycTOAHlBAyUnASD\nNQahXAikEE1frUQlAZHkohQQS1Raeqm3t6BNG65eCaiXtPSlyNVio1JD/UH4ZYiCDSnBeqWAnJCE\nmJKU8DM54UI0OUjJkZwkz/1jr0n2mbP3/DqzZ+bMPO/Xa5yZNXvP3htP9jNrrWetJTPDOeecy9Ko\nRp+Ac8651ufBxjnnXOY82DjnnMucBxvnnHOZ82DjnHMucx5snHPOZc6DjXMFJP1E0h+nfPZjSfMz\nOu6Vkl6W9J+SjsriGNWS9B5J22LvN0p6TwNPyY0woxt9As4Nh6TngT82s3+px/HM7JwsvldSDvgS\nMMPM1mdxjArPx4ApZrYl6XMzO6nOp+RGOK/ZONccjgEOBTZWuqMiI+LfsiT/gdumRsQfqHPVkPQn\nkrZI2ilphaQJsc/eLelxSa+G53enfMexkp6U9Jfh/YEmNkkfk/QzSX8naZek5ySdE9v3eEk/lfSa\npH+R9DVJ30k4xjuAzeFtn6TVpc4xnMcXJT0M7AbelvC97wzb9YVmr/ML9v/j2PuPSfpZeP3TULw+\nNOldlPDdz0t6X3g9StJCSc9I+pWkOyQdGT6bLMkkXS7pRWC1pEMlfSds2xeu7Zik//6udXiwcS1J\n0izgBuAPgWOBF4Dbw2dHAvcBNwNHETVf3VfYTyJpMvCvwFfN7O9SDnUmUaB4C/C3wLckKXz2PeDn\n4RiLgD9K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0x7AnARsCy8ngk8Z2bPEM2ldm5sv6lE09ZPA14HPgu8\nz8xOA3qI1pQB+KqZ/a6Z/Q7QCZxXxjk5N2webJyrMUlfU7QC5+OhaJWZFa5BlMiiqfX/GfhgaHb7\nAAfn1LqEaEJLwnO8Ke0Fi9ZGgWhCzxOBh8MSBvOBt4bPzlK0CuYGohrZSZVfoXOV8yUGnBu+jcCH\n8m/M7JOS3kJUo4CoplGJZcAniWY8ftzMXpPUEY5xvqTPEM2vdVR+AsiCY4gowA3q1wnNbn9PNBfX\nVkmLiOYjcy5zXrNxbvhWA4dKujJWNnYY3/cToqW//4SDTWjvA9ab2SQzm2xmbyVagmFewv6PAjMl\nvR1A0lhJ7+BgYPll6MP5cMK+zmXCg41zw2TRbLbzgD+Q9JyknxMtA/zXKbtMlbQt9riw4Pv2AT8C\nzgnPEDWZ/aDge+4GPpJwPjuIkgi+L+lJouBzgkXr5nyDaKbh5cDjhfs6lxWf9dk551zmvGbjnHMu\ncx5snHPOZc6DjXPOucx5sHHOOZc5DzbOOecy58HGOedc5jzYOOecy9z/ByNFSCtQgBV7AAAAAElF\nTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x115661278>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(train.GrLivArea, train.SalePrice)\n",
    "plt.title(\"Looking for outliers\")\n",
    "plt.xlabel(\"GrLivArea\")\n",
    "plt.ylabel(\"SalePrice\")\n",
    "plt.show()\n",
    "\n",
    "train = train[train.GrLivArea < 4000]\n",
    "temp = train.reindex()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 有些特征，用median/mean or most common value 填充没有意义\n",
    "# 因为特征工程对训练集和测试都需要进行，因此我们定义成函数，将数据集以参数形式传递\n",
    "def process_missvalue_by_meaning (df):\n",
    "    # Alley : data description says NA means \"no alley access\"\n",
    "    df.loc[:, \"Alley\"] = df.loc[:, \"Alley\"].fillna(\"None\")\n",
    "\n",
    "    # BedroomAbvGr : NA most likely means 0\n",
    "    df.loc[:, \"BedroomAbvGr\"] = df.loc[:, \"BedroomAbvGr\"].fillna(0)\n",
    "\n",
    "    # BsmtQual etc : data description says NA for basement features is \"no basement\"\n",
    "    df.loc[:, \"BsmtQual\"] = df.loc[:, \"BsmtQual\"].fillna(\"No\")\n",
    "    df.loc[:, \"BsmtCond\"] = df.loc[:, \"BsmtCond\"].fillna(\"No\")\n",
    "    df.loc[:, \"BsmtExposure\"] = df.loc[:, \"BsmtExposure\"].fillna(\"No\")\n",
    "    df.loc[:, \"BsmtFinType1\"] = df.loc[:, \"BsmtFinType1\"].fillna(\"No\")\n",
    "    df.loc[:, \"BsmtFinType2\"] = df.loc[:, \"BsmtFinType2\"].fillna(\"No\")\n",
    "    df.loc[:, \"BsmtFullBath\"] = df.loc[:, \"BsmtFullBath\"].fillna(0)\n",
    "    df.loc[:, \"BsmtHalfBath\"] = df.loc[:, \"BsmtHalfBath\"].fillna(0)\n",
    "    df.loc[:, \"BsmtUnfSF\"] = df.loc[:, \"BsmtUnfSF\"].fillna(0)\n",
    "\n",
    "    # CentralAir : NA most likely means No\n",
    "    df.loc[:, \"CentralAir\"] = df.loc[:, \"CentralAir\"].fillna(\"No\")\n",
    "\n",
    "    # Condition : NA most likely means Normal，靠近主干道或铁路\n",
    "    df.loc[:, \"Condition1\"] = df.loc[:, \"Condition1\"].fillna(\"Norm\")\n",
    "    df.loc[:, \"Condition2\"] = df.loc[:, \"Condition2\"].fillna(\"Norm\")\n",
    "\n",
    "    # EnclosedPorch : NA most likely means no enclosed porch\n",
    "    df.loc[:, \"EnclosedPorch\"] = df.loc[:, \"EnclosedPorch\"].fillna(0)\n",
    "\n",
    "    # External stuff : NA most likely means average\n",
    "    df.loc[:, \"ExterCond\"] = df.loc[:, \"ExterCond\"].fillna(\"TA\")\n",
    "    df.loc[:, \"ExterQual\"] = df.loc[:, \"ExterQual\"].fillna(\"TA\")\n",
    "\n",
    "    # Fence : data description says NA means \"no fence\"\n",
    "    df.loc[:, \"Fence\"] = df.loc[:, \"Fence\"].fillna(\"No\")\n",
    "\n",
    "    # FireplaceQu : data description says NA means \"no fireplace\"\n",
    "    df.loc[:, \"FireplaceQu\"] = df.loc[:, \"FireplaceQu\"].fillna(\"No\")\n",
    "    df.loc[:, \"Fireplaces\"] = df.loc[:, \"Fireplaces\"].fillna(0)\n",
    "\n",
    "    # Functional : data description says NA means typical，家用（Home）功能性评级\n",
    "    df.loc[:, \"Functional\"] = df.loc[:, \"Functional\"].fillna(\"Typ\")\n",
    "\n",
    "    # GarageType etc : data description says NA for garage features is \"no garage\"\n",
    "    df.loc[:, \"GarageType\"] = df.loc[:, \"GarageType\"].fillna(\"No\")\n",
    "    df.loc[:, \"GarageFinish\"] = df.loc[:, \"GarageFinish\"].fillna(\"No\")\n",
    "    df.loc[:, \"GarageQual\"] = df.loc[:, \"GarageQual\"].fillna(\"No\")\n",
    "    df.loc[:, \"GarageCond\"] = df.loc[:, \"GarageCond\"].fillna(\"No\")\n",
    "    df.loc[:, \"GarageArea\"] = df.loc[:, \"GarageArea\"].fillna(0)\n",
    "    df.loc[:, \"GarageCars\"] = df.loc[:, \"GarageCars\"].fillna(0)\n",
    "\n",
    "    # HalfBath : NA most likely means no half baths above grade\n",
    "    df.loc[:, \"HalfBath\"] = df.loc[:, \"HalfBath\"].fillna(0)\n",
    "\n",
    "    # HeatingQC : NA most likely means typical\n",
    "    df.loc[:, \"HeatingQC\"] = df.loc[:, \"HeatingQC\"].fillna(\"TA\")\n",
    "\n",
    "    # KitchenAbvGr : NA most likely means 0\n",
    "    df.loc[:, \"KitchenAbvGr\"] = df.loc[:, \"KitchenAbvGr\"].fillna(0)\n",
    "\n",
    "    # KitchenQual : NA most likely means typical\n",
    "    df.loc[:, \"KitchenQual\"] = df.loc[:, \"KitchenQual\"].fillna(\"TA\")\n",
    "\n",
    "    # LotFrontage : NA most likely means no lot frontage\n",
    "    df.loc[:, \"LotFrontage\"] = df.loc[:, \"LotFrontage\"].fillna(0)\n",
    "\n",
    "    # LotShape : NA most likely means regular\n",
    "    df.loc[:, \"LotShape\"] = df.loc[:, \"LotShape\"].fillna(\"Reg\")\n",
    "\n",
    "    # MasVnrType : NA most likely means no veneer，表层砌体（Masonry veneer）类型\n",
    "    df.loc[:, \"MasVnrType\"] = df.loc[:, \"MasVnrType\"].fillna(\"None\")\n",
    "    df.loc[:, \"MasVnrArea\"] = df.loc[:, \"MasVnrArea\"].fillna(0)\n",
    "\n",
    "    # MiscFeature : data description says NA means \"no misc feature\"\n",
    "    df.loc[:, \"MiscFeature\"] = df.loc[:, \"MiscFeature\"].fillna(\"No\")\n",
    "    df.loc[:, \"MiscVal\"] = df.loc[:, \"MiscVal\"].fillna(0)\n",
    "\n",
    "    # OpenPorchSF : NA most likely means no open porch\n",
    "    df.loc[:, \"OpenPorchSF\"] = df.loc[:, \"OpenPorchSF\"].fillna(0)\n",
    "\n",
    "    # PavedDrive : NA most likely means not paved\n",
    "    df.loc[:, \"PavedDrive\"] = df.loc[:, \"PavedDrive\"].fillna(\"No\")\n",
    "\n",
    "    # PoolQC : data description says NA means \"no pool\"\n",
    "    df.loc[:, \"PoolQC\"] = df.loc[:, \"PoolQC\"].fillna(\"No\")\n",
    "    df.loc[:, \"PoolArea\"] = df.loc[:, \"PoolArea\"].fillna(0)\n",
    "\n",
    "    # SaleCondition : NA most likely means normal sale\n",
    "    df.loc[:, \"SaleCondition\"] = df.loc[:, \"SaleCondition\"].fillna(\"Normal\")\n",
    "\n",
    "    # ScreenPorch : NA most likely means no screen porch，观景门廊\n",
    "    df.loc[:, \"ScreenPorch\"] = df.loc[:, \"ScreenPorch\"].fillna(0)\n",
    "\n",
    "    # TotRmsAbvGrd : NA most likely means 0\n",
    "    df.loc[:, \"TotRmsAbvGrd\"] = df.loc[:, \"TotRmsAbvGrd\"].fillna(0)\n",
    "\n",
    "    # Utilities : NA most likely means all public utilities\n",
    "    df.loc[:, \"Utilities\"] = df.loc[:, \"Utilities\"].fillna(\"AllPub\")\n",
    "\n",
    "    # WoodDeckSF : NA most likely means no wood deck\n",
    "    df.loc[:, \"WoodDeckSF\"] = df.loc[:, \"WoodDeckSF\"].fillna(0)\n",
    "    \n",
    "    return df\n",
    "    \n",
    "train = process_missvalue_by_meaning(train)\n",
    "test = process_missvalue_by_meaning(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>MSZoning</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>LandContour</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LotConfig</th>\n",
       "      <th>...</th>\n",
       "      <th>PoolArea</th>\n",
       "      <th>PoolQC</th>\n",
       "      <th>Fence</th>\n",
       "      <th>MiscFeature</th>\n",
       "      <th>MiscVal</th>\n",
       "      <th>MoSold</th>\n",
       "      <th>YrSold</th>\n",
       "      <th>SaleType</th>\n",
       "      <th>SaleCondition</th>\n",
       "      <th>SalePrice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>65.000</td>\n",
       "      <td>8450</td>\n",
       "      <td>Pave</td>\n",
       "      <td>None</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>208500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20</td>\n",
       "      <td>RL</td>\n",
       "      <td>80.000</td>\n",
       "      <td>9600</td>\n",
       "      <td>Pave</td>\n",
       "      <td>None</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>2007</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>181500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>68.000</td>\n",
       "      <td>11250</td>\n",
       "      <td>Pave</td>\n",
       "      <td>None</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>223500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>70</td>\n",
       "      <td>RL</td>\n",
       "      <td>60.000</td>\n",
       "      <td>9550</td>\n",
       "      <td>Pave</td>\n",
       "      <td>None</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Corner</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2006</td>\n",
       "      <td>WD</td>\n",
       "      <td>Abnorml</td>\n",
       "      <td>140000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>84.000</td>\n",
       "      <td>14260</td>\n",
       "      <td>Pave</td>\n",
       "      <td>None</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>250000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 80 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   MSSubClass MSZoning  LotFrontage  LotArea Street Alley LotShape  \\\n",
       "0          60       RL       65.000     8450   Pave  None      Reg   \n",
       "1          20       RL       80.000     9600   Pave  None      Reg   \n",
       "2          60       RL       68.000    11250   Pave  None      IR1   \n",
       "3          70       RL       60.000     9550   Pave  None      IR1   \n",
       "4          60       RL       84.000    14260   Pave  None      IR1   \n",
       "\n",
       "  LandContour Utilities LotConfig    ...     PoolArea PoolQC Fence  \\\n",
       "0         Lvl    AllPub    Inside    ...            0     No    No   \n",
       "1         Lvl    AllPub       FR2    ...            0     No    No   \n",
       "2         Lvl    AllPub    Inside    ...            0     No    No   \n",
       "3         Lvl    AllPub    Corner    ...            0     No    No   \n",
       "4         Lvl    AllPub       FR2    ...            0     No    No   \n",
       "\n",
       "  MiscFeature MiscVal MoSold  YrSold  SaleType  SaleCondition  SalePrice  \n",
       "0          No       0      2    2008        WD         Normal     208500  \n",
       "1          No       0      5    2007        WD         Normal     181500  \n",
       "2          No       0      9    2008        WD         Normal     223500  \n",
       "3          No       0      2    2006        WD        Abnorml     140000  \n",
       "4          No       0     12    2008        WD         Normal     250000  \n",
       "\n",
       "[5 rows x 80 columns]"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Some numerical features are actually really categories\n",
    "# MSSubClass：建筑类\n",
    "#MoSold：销售月份\n",
    "\n",
    "def numberical2cat(df):\n",
    "    df.replace({\"MSSubClass\" : {20 : \"SC20\", 30 : \"SC30\", 40 : \"SC40\", 45 : \"SC45\", \n",
    "                                       50 : \"SC50\", 60 : \"SC60\", 70 : \"SC70\", 75 : \"SC75\", \n",
    "                                       80 : \"SC80\", 85 : \"SC85\", 90 : \"SC90\", 120 : \"SC120\", \n",
    "                                       150 : \"SC150\", 160 : \"SC160\", 180 : \"SC180\", 190 : \"SC190\"},\n",
    "                       \"MoSold\" : {1 : \"Jan\", 2 : \"Feb\", 3 : \"Mar\", 4 : \"Apr\", 5 : \"May\", 6 : \"Jun\",\n",
    "                                   7 : \"Jul\", 8 : \"Aug\", 9 : \"Sep\", 10 : \"Oct\", 11 : \"Nov\", 12 : \"Dec\"}\n",
    "                      }, inplace = True)\n",
    "\n",
    "    return df\n",
    "train = numberical2cat(train)\n",
    "test = numberical2cat(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>MSZoning</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>LandContour</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LotConfig</th>\n",
       "      <th>...</th>\n",
       "      <th>PoolArea</th>\n",
       "      <th>PoolQC</th>\n",
       "      <th>Fence</th>\n",
       "      <th>MiscFeature</th>\n",
       "      <th>MiscVal</th>\n",
       "      <th>MoSold</th>\n",
       "      <th>YrSold</th>\n",
       "      <th>SaleType</th>\n",
       "      <th>SaleCondition</th>\n",
       "      <th>SalePrice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>65.000</td>\n",
       "      <td>8450</td>\n",
       "      <td>Pave</td>\n",
       "      <td>None</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>Feb</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>208500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SC20</td>\n",
       "      <td>RL</td>\n",
       "      <td>80.000</td>\n",
       "      <td>9600</td>\n",
       "      <td>Pave</td>\n",
       "      <td>None</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>May</td>\n",
       "      <td>2007</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>181500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>68.000</td>\n",
       "      <td>11250</td>\n",
       "      <td>Pave</td>\n",
       "      <td>None</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>Sep</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>223500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SC70</td>\n",
       "      <td>RL</td>\n",
       "      <td>60.000</td>\n",
       "      <td>9550</td>\n",
       "      <td>Pave</td>\n",
       "      <td>None</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Corner</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>Feb</td>\n",
       "      <td>2006</td>\n",
       "      <td>WD</td>\n",
       "      <td>Abnorml</td>\n",
       "      <td>140000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>84.000</td>\n",
       "      <td>14260</td>\n",
       "      <td>Pave</td>\n",
       "      <td>None</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>Dec</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>250000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 80 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  MSSubClass MSZoning  LotFrontage  LotArea Street Alley LotShape LandContour  \\\n",
       "0       SC60       RL       65.000     8450   Pave  None      Reg         Lvl   \n",
       "1       SC20       RL       80.000     9600   Pave  None      Reg         Lvl   \n",
       "2       SC60       RL       68.000    11250   Pave  None      IR1         Lvl   \n",
       "3       SC70       RL       60.000     9550   Pave  None      IR1         Lvl   \n",
       "4       SC60       RL       84.000    14260   Pave  None      IR1         Lvl   \n",
       "\n",
       "  Utilities LotConfig    ...     PoolArea PoolQC Fence MiscFeature MiscVal  \\\n",
       "0    AllPub    Inside    ...            0     No    No          No       0   \n",
       "1    AllPub       FR2    ...            0     No    No          No       0   \n",
       "2    AllPub    Inside    ...            0     No    No          No       0   \n",
       "3    AllPub    Corner    ...            0     No    No          No       0   \n",
       "4    AllPub       FR2    ...            0     No    No          No       0   \n",
       "\n",
       "  MoSold  YrSold  SaleType  SaleCondition  SalePrice  \n",
       "0    Feb    2008        WD         Normal     208500  \n",
       "1    May    2007        WD         Normal     181500  \n",
       "2    Sep    2008        WD         Normal     223500  \n",
       "3    Feb    2006        WD        Abnorml     140000  \n",
       "4    Dec    2008        WD         Normal     250000  \n",
       "\n",
       "[5 rows x 80 columns]"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Encode some categorical features as ordered numbers when there is information in the order\n",
    "def cat2numberical(df):\n",
    "    df.replace({\"Alley\" : {\"None\":0, \"Grvl\" : 1, \"Pave\" : 2},\n",
    "                \"BsmtCond\" : {\"No\" : 0, \"Po\" : 1, \"Fa\" : 2, \"TA\" : 3, \"Gd\" : 4, \"Ex\" : 5},\n",
    "                \"BsmtExposure\" : {\"No\" : 0, \"Mn\" : 1, \"Av\": 2, \"Gd\" : 3},\n",
    "                \"BsmtFinType1\" : {\"No\" : 0, \"Unf\" : 1, \"LwQ\": 2, \"Rec\" : 3, \"BLQ\" : 4, \n",
    "                                         \"ALQ\" : 5, \"GLQ\" : 6},\n",
    "                \"BsmtFinType2\" : {\"No\" : 0, \"Unf\" : 1, \"LwQ\": 2, \"Rec\" : 3, \"BLQ\" : 4, \n",
    "                                         \"ALQ\" : 5, \"GLQ\" : 6},\n",
    "                \"BsmtQual\" : {\"No\" : 0, \"Po\" : 1, \"Fa\" : 2, \"TA\": 3, \"Gd\" : 4, \"Ex\" : 5},\n",
    "                \"ExterCond\" : {\"Po\" : 1, \"Fa\" : 2, \"TA\": 3, \"Gd\": 4, \"Ex\" : 5},\n",
    "                \"ExterQual\" : {\"Po\" : 1, \"Fa\" : 2, \"TA\": 3, \"Gd\": 4, \"Ex\" : 5},\n",
    "                \"FireplaceQu\" : {\"No\" : 0, \"Po\" : 1, \"Fa\" : 2, \"TA\" : 3, \"Gd\" : 4, \"Ex\" : 5},\n",
    "                \"Functional\" : {\"Sal\" : 1, \"Sev\" : 2, \"Maj2\" : 3, \"Maj1\" : 4, \"Mod\": 5, \n",
    "                                       \"Min2\" : 6, \"Min1\" : 7, \"Typ\" : 8},\n",
    "                \"GarageCond\" : {\"No\" : 0, \"Po\" : 1, \"Fa\" : 2, \"TA\" : 3, \"Gd\" : 4, \"Ex\" : 5},\n",
    "                \"GarageQual\" : {\"No\" : 0, \"Po\" : 1, \"Fa\" : 2, \"TA\" : 3, \"Gd\" : 4, \"Ex\" : 5},\n",
    "                \"HeatingQC\" : {\"Po\" : 1, \"Fa\" : 2, \"TA\" : 3, \"Gd\" : 4, \"Ex\" : 5},\n",
    "                \"KitchenQual\" : {\"Po\" : 1, \"Fa\" : 2, \"TA\" : 3, \"Gd\" : 4, \"Ex\" : 5},\n",
    "                \"LandSlope\" : {\"Sev\" : 1, \"Mod\" : 2, \"Gtl\" : 3},\n",
    "                \"LotShape\" : {\"IR3\" : 1, \"IR2\" : 2, \"IR1\" : 3, \"Reg\" : 4},\n",
    "                \"PavedDrive\" : {\"N\" : 0, \"P\" : 1, \"Y\" : 2},\n",
    "                \"PoolQC\" : {\"No\" : 0, \"Fa\" : 1, \"TA\" : 2, \"Gd\" : 3, \"Ex\" : 4},\n",
    "                \"Street\" : {\"Grvl\" : 1, \"Pave\" : 2},\n",
    "                \"Utilities\" : {\"ELO\" : 1, \"NoSeWa\" : 2, \"NoSewr\" : 3, \"AllPub\" : 4}},\n",
    "                       inplace = True\n",
    "                     )\n",
    "    return df\n",
    "\n",
    "train = cat2numberical(train)\n",
    "test = cat2numberical(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>MSZoning</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>LandContour</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LotConfig</th>\n",
       "      <th>...</th>\n",
       "      <th>PoolArea</th>\n",
       "      <th>PoolQC</th>\n",
       "      <th>Fence</th>\n",
       "      <th>MiscFeature</th>\n",
       "      <th>MiscVal</th>\n",
       "      <th>MoSold</th>\n",
       "      <th>YrSold</th>\n",
       "      <th>SaleType</th>\n",
       "      <th>SaleCondition</th>\n",
       "      <th>SalePrice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>65.000</td>\n",
       "      <td>8450</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>Feb</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>208500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SC20</td>\n",
       "      <td>RL</td>\n",
       "      <td>80.000</td>\n",
       "      <td>9600</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>May</td>\n",
       "      <td>2007</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>181500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>68.000</td>\n",
       "      <td>11250</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>Sep</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>223500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SC70</td>\n",
       "      <td>RL</td>\n",
       "      <td>60.000</td>\n",
       "      <td>9550</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Corner</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>Feb</td>\n",
       "      <td>2006</td>\n",
       "      <td>WD</td>\n",
       "      <td>Abnorml</td>\n",
       "      <td>140000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>84.000</td>\n",
       "      <td>14260</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "      <td>Dec</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>250000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 80 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  MSSubClass MSZoning  LotFrontage  LotArea  Street  Alley  LotShape  \\\n",
       "0       SC60       RL       65.000     8450       2      0         4   \n",
       "1       SC20       RL       80.000     9600       2      0         4   \n",
       "2       SC60       RL       68.000    11250       2      0         3   \n",
       "3       SC70       RL       60.000     9550       2      0         3   \n",
       "4       SC60       RL       84.000    14260       2      0         3   \n",
       "\n",
       "  LandContour  Utilities LotConfig    ...      PoolArea PoolQC Fence  \\\n",
       "0         Lvl          4    Inside    ...             0      0    No   \n",
       "1         Lvl          4       FR2    ...             0      0    No   \n",
       "2         Lvl          4    Inside    ...             0      0    No   \n",
       "3         Lvl          4    Corner    ...             0      0    No   \n",
       "4         Lvl          4       FR2    ...             0      0    No   \n",
       "\n",
       "  MiscFeature MiscVal MoSold  YrSold  SaleType  SaleCondition  SalePrice  \n",
       "0          No       0    Feb    2008        WD         Normal     208500  \n",
       "1          No       0    May    2007        WD         Normal     181500  \n",
       "2          No       0    Sep    2008        WD         Normal     223500  \n",
       "3          No       0    Feb    2006        WD        Abnorml     140000  \n",
       "4          No       0    Dec    2008        WD         Normal     250000  \n",
       "\n",
       "[5 rows x 80 columns]"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 还可以通过以下方式创建一些新特征 : \n",
    "\n",
    " 1. 简化已有特征\n",
    " 2. 联合已有特征\n",
    " 3. 现有重要特征（top 10）的多项式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create new features\n",
    "# 1* Simplifications of existing features\n",
    "# 合并类别\n",
    "def simplify(df):\n",
    "    df[\"SimplOverallQual\"] = df.OverallQual.replace({1 : 1, 2 : 1, 3 : 1, # bad\n",
    "                                                    4 : 2, 5 : 2, 6 : 2, # average\n",
    "                                                    7 : 3, 8 : 3, 9 : 3, 10 : 3 # good\n",
    "                                                    }, inplace = True)\n",
    "    df[\"SimplOverallCond\"] = df.OverallCond.replace({1 : 1, 2 : 1, 3 : 1, # bad\n",
    "                                                    4 : 2, 5 : 2, 6 : 2, # average\n",
    "                                                    7 : 3, 8 : 3, 9 : 3, 10 : 3 # good\n",
    "                                                    },inplace = True)\n",
    "    df[\"SimplPoolQC\"] = df.PoolQC.replace({1 : 1, 2 : 1, # average\n",
    "                                           3 : 2, 4 : 2 # good\n",
    "                                          },inplace = True)\n",
    "    df[\"SimplGarageCond\"] = df.GarageCond.replace({1 : 1, # bad\n",
    "                                                2 : 1, 3 : 1, # average\n",
    "                                                4 : 2, 5 : 2 # good\n",
    "                                                        },inplace = True)\n",
    "    df[\"SimplGarageQual\"] = df.GarageQual.replace({1 : 1, # bad\n",
    "                                                    2 : 1, 3 : 1, # average\n",
    "                                                    4 : 2, 5 : 2 # good\n",
    "                                                    },inplace = True)\n",
    "    df[\"SimplFireplaceQu\"] = df.FireplaceQu.replace({1 : 1, # bad\n",
    "                                                           2 : 1, 3 : 1, # average\n",
    "                                                           4 : 2, 5 : 2 # good\n",
    "                                                          },inplace = True)\n",
    "    df[\"SimplFireplaceQu\"] = df.FireplaceQu.replace({1 : 1, # bad\n",
    "                                                           2 : 1, 3 : 1, # average\n",
    "                                                           4 : 2, 5 : 2 # good\n",
    "                                                          },inplace = True)\n",
    "    df[\"SimplFunctional\"] = df.Functional.replace({1 : 1, 2 : 1, # bad\n",
    "                                                         3 : 2, 4 : 2, # major\n",
    "                                                         5 : 3, 6 : 3, 7 : 3, # minor\n",
    "                                                         8 : 4 # typical\n",
    "                                                        },inplace = True)\n",
    "    df[\"SimplKitchenQual\"] = df.KitchenQual.replace({1 : 1, # bad\n",
    "                                                           2 : 1, 3 : 1, # average\n",
    "                                                           4 : 2, 5 : 2 # good\n",
    "                                                          },inplace = True)\n",
    "    df[\"SimplHeatingQC\"] = df.HeatingQC.replace({1 : 1, # bad\n",
    "                                                       2 : 1, 3 : 1, # average\n",
    "                                                       4 : 2, 5 : 2 # good\n",
    "                                                      },inplace = True)\n",
    "    df[\"SimplBsmtFinType1\"] = df.BsmtFinType1.replace({1 : 1, # unfinished\n",
    "                                                             2 : 1, 3 : 1, # rec room\n",
    "                                                             4 : 2, 5 : 2, 6 : 2 # living quarters\n",
    "                                                            },inplace = True)\n",
    "    df[\"SimplBsmtFinType2\"] = df.BsmtFinType2.replace({1 : 1, # unfinished\n",
    "                                                             2 : 1, 3 : 1, # rec room\n",
    "                                                             4 : 2, 5 : 2, 6 : 2 # living quarters\n",
    "                                                            },inplace = True)\n",
    "    df[\"SimplBsmtCond\"] = df.BsmtCond.replace({1 : 1, # bad\n",
    "                                                     2 : 1, 3 : 1, # average\n",
    "                                                     4 : 2, 5 : 2 # good\n",
    "                                                    },inplace = True)\n",
    "    df[\"SimplBsmtQual\"] = df.BsmtQual.replace({1 : 1, # bad\n",
    "                                                     2 : 1, 3 : 1, # average\n",
    "                                                     4 : 2, 5 : 2 # good\n",
    "                                                    },inplace = True)\n",
    "    df[\"SimplExterCond\"] = df.ExterCond.replace({1 : 1, # bad\n",
    "                                                       2 : 1, 3 : 1, # average\n",
    "                                                       4 : 2, 5 : 2 # good\n",
    "                                                      },inplace = True)\n",
    "    df[\"SimplExterQual\"] = df.ExterQual.replace({1 : 1, # bad\n",
    "                                                       2 : 1, 3 : 1, # average\n",
    "                                                       4 : 2, 5 : 2 # good\n",
    "                                                      },inplace = True)\n",
    "    return df\n",
    "\n",
    "train = simplify(train)\n",
    "test = simplify(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>MSZoning</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>LandContour</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LotConfig</th>\n",
       "      <th>...</th>\n",
       "      <th>SimplFireplaceQu</th>\n",
       "      <th>SimplFunctional</th>\n",
       "      <th>SimplKitchenQual</th>\n",
       "      <th>SimplHeatingQC</th>\n",
       "      <th>SimplBsmtFinType1</th>\n",
       "      <th>SimplBsmtFinType2</th>\n",
       "      <th>SimplBsmtCond</th>\n",
       "      <th>SimplBsmtQual</th>\n",
       "      <th>SimplExterCond</th>\n",
       "      <th>SimplExterQual</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>65.000</td>\n",
       "      <td>8450</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SC20</td>\n",
       "      <td>RL</td>\n",
       "      <td>80.000</td>\n",
       "      <td>9600</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>68.000</td>\n",
       "      <td>11250</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SC70</td>\n",
       "      <td>RL</td>\n",
       "      <td>60.000</td>\n",
       "      <td>9550</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Corner</td>\n",
       "      <td>...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>84.000</td>\n",
       "      <td>14260</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 95 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  MSSubClass MSZoning  LotFrontage  LotArea  Street  Alley  LotShape  \\\n",
       "0       SC60       RL       65.000     8450       2      0         4   \n",
       "1       SC20       RL       80.000     9600       2      0         4   \n",
       "2       SC60       RL       68.000    11250       2      0         3   \n",
       "3       SC70       RL       60.000     9550       2      0         3   \n",
       "4       SC60       RL       84.000    14260       2      0         3   \n",
       "\n",
       "  LandContour  Utilities LotConfig       ...        SimplFireplaceQu  \\\n",
       "0         Lvl          4    Inside       ...                    None   \n",
       "1         Lvl          4       FR2       ...                    None   \n",
       "2         Lvl          4    Inside       ...                    None   \n",
       "3         Lvl          4    Corner       ...                    None   \n",
       "4         Lvl          4       FR2       ...                    None   \n",
       "\n",
       "  SimplFunctional SimplKitchenQual SimplHeatingQC SimplBsmtFinType1  \\\n",
       "0            None             None           None              None   \n",
       "1            None             None           None              None   \n",
       "2            None             None           None              None   \n",
       "3            None             None           None              None   \n",
       "4            None             None           None              None   \n",
       "\n",
       "  SimplBsmtFinType2  SimplBsmtCond  SimplBsmtQual  SimplExterCond  \\\n",
       "0              None           None           None            None   \n",
       "1              None           None           None            None   \n",
       "2              None           None           None            None   \n",
       "3              None           None           None            None   \n",
       "4              None           None           None            None   \n",
       "\n",
       "   SimplExterQual  \n",
       "0            None  \n",
       "1            None  \n",
       "2            None  \n",
       "3            None  \n",
       "4            None  \n",
       "\n",
       "[5 rows x 95 columns]"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 2* Combinations of existing features\n",
    "def Combine(df):\n",
    "    # Overall quality of the house 房子的整体质量 = 整体材质和完成品质 x 总体条件评级\n",
    "    df[\"OverallGrade\"] = df[\"OverallQual\"] * df[\"OverallCond\"]\n",
    "    # Overall quality of the garage 车库整体质量 = 车库质量 x 车库条件\n",
    "    df[\"GarageGrade\"] = df[\"GarageQual\"] * df[\"GarageCond\"]\n",
    "    # Overall quality of the exterior 外观整体质量 = 外部材料的质量 x 外部材料的现状\n",
    "    df[\"ExterGrade\"] = df[\"ExterQual\"] * df[\"ExterCond\"]\n",
    "    # Overall kitchen score 厨房整体评分 = 厨房数目 x 厨房质量\n",
    "    df[\"KitchenScore\"] = df[\"KitchenAbvGr\"] * df[\"KitchenQual\"]\n",
    "    # Overall fireplace score 整体壁炉评分 = 壁炉的数目 x 壁炉质量\n",
    "    df[\"FireplaceScore\"] = df[\"Fireplaces\"] * df[\"FireplaceQu\"]\n",
    "    # Overall garage score 整个车库评分 = 车库大小 x 车库质量\n",
    "    df[\"GarageScore\"] = df[\"GarageArea\"] * df[\"GarageQual\"]\n",
    "    # Overall pool score 整体泳池评分 = 游泳池面积 x 游泳池质量\n",
    "    df[\"PoolScore\"] = df[\"PoolArea\"] * df[\"PoolQC\"]\n",
    "    # Simplified overall quality of the house 简化的房屋整体质量 = 简化的整体材质和完成品质 x 简化的总体条件评级\n",
    "    df[\"SimplOverallGrade\"] = df[\"SimplOverallQual\"] * df[\"SimplOverallCond\"]\n",
    "    # Simplified overall quality of the exterior 简化的外观整体质量 = 简化的外部材料的质量 x 简化的外部材料的现状\n",
    "    df[\"SimplExterGrade\"] = df[\"SimplExterQual\"] * df[\"SimplExterCond\"]\n",
    "    # Simplified overall pool score 简化的整体泳池评分 = 简化的游泳池面积 x 简化的游泳池质量\n",
    "    df[\"SimplPoolScore\"] = df[\"PoolArea\"] * df[\"SimplPoolQC\"]\n",
    "    # Simplified overall garage score 简化的车库整体质量 = 简化的车库质量 x 简化的车库条件\n",
    "    df[\"SimplGarageScore\"] = df[\"GarageArea\"] * df[\"SimplGarageQual\"]\n",
    "    # Simplified overall fireplace score 简化的整体壁炉评分 = 简化的壁炉的数目 x 简化的壁炉质量\n",
    "    df[\"SimplFireplaceScore\"] = df[\"Fireplaces\"] * df[\"SimplFireplaceQu\"]\n",
    "    # Simplified overall kitchen score 简化的厨房整体评分 = 简化的厨房数目 x 简化的厨房质量\n",
    "    df[\"SimplKitchenScore\"] = df[\"KitchenAbvGr\"] * df[\"SimplKitchenQual\"]\n",
    "    # Total number of bathrooms 整体的浴室面积 = 地下室全浴室数目 + 地下室半浴室数目 * 0.5 + 地上全浴室数 + 地上半浴室数目 * 0.5\n",
    "    df[\"TotalBath\"] = df[\"BsmtFullBath\"] + (0.5 * df[\"BsmtHalfBath\"]) + \\\n",
    "    df[\"FullBath\"] + (0.5 * df[\"HalfBath\"])\n",
    "    # Total SF for house (incl. basement) 总面积 = 地上居住面积 x 地下室总面积\n",
    "    df[\"AllSF\"] = df[\"GrLivArea\"] + df[\"TotalBsmtSF\"]\n",
    "    # Total SF for 1st + 2nd floors 总楼层面积 = 第一层的面积 x 第二层的面积\n",
    "    df[\"AllFlrsSF\"] = df[\"1stFlrSF\"] + df[\"2ndFlrSF\"]\n",
    "    # Total SF for porch 总门廊的面积 = 开放门廊面积 + 封闭门廊面积 + 三季门廊面积 + 观景门廊\n",
    "    df[\"AllPorchSF\"] = df[\"OpenPorchSF\"] + df[\"EnclosedPorch\"] + \\\n",
    "    df[\"3SsnPorch\"] + df[\"ScreenPorch\"]\n",
    "    # Has masonry veneer or not 表层砌体类型 有 或者 无\n",
    "    df[\"HasMasVnr\"] = df.MasVnrType.replace({\"BrkCmn\" : 1, \"BrkFace\" : 1, \"CBlock\" : 1, \n",
    "                                                   \"Stone\" : 1, \"None\" : 0})\n",
    "    # House completed before sale or not 销售条件 出售 或 没有出售\n",
    "    df[\"BoughtOffPlan\"] = df.SaleCondition.replace({\"Abnorml\" : 0, \"Alloca\" : 0, \"AdjLand\" : 0, \n",
    "                                                          \"Family\" : 0, \"Normal\" : 0, \"Partial\" : 1})\n",
    "    \n",
    "    return df\n",
    "\n",
    "#对训练集和测试集分别进行编码\n",
    "train = Combine(train)\n",
    "test = Combine(test)\n",
    "\n",
    "train.to_csv(\"AmesHouse_temp_train.csv\")\n",
    "test.to_csv(\"AmesHouse_temp_test.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>MSZoning</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>LandContour</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LotConfig</th>\n",
       "      <th>...</th>\n",
       "      <th>SimplPoolScore</th>\n",
       "      <th>SimplGarageScore</th>\n",
       "      <th>SimplFireplaceScore</th>\n",
       "      <th>SimplKitchenScore</th>\n",
       "      <th>TotalBath</th>\n",
       "      <th>AllSF</th>\n",
       "      <th>AllFlrsSF</th>\n",
       "      <th>AllPorchSF</th>\n",
       "      <th>HasMasVnr</th>\n",
       "      <th>BoughtOffPlan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>65.000</td>\n",
       "      <td>8450</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.500</td>\n",
       "      <td>2566</td>\n",
       "      <td>1710</td>\n",
       "      <td>61</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SC20</td>\n",
       "      <td>RL</td>\n",
       "      <td>80.000</td>\n",
       "      <td>9600</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.500</td>\n",
       "      <td>2524</td>\n",
       "      <td>1262</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>68.000</td>\n",
       "      <td>11250</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.500</td>\n",
       "      <td>2706</td>\n",
       "      <td>1786</td>\n",
       "      <td>42</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SC70</td>\n",
       "      <td>RL</td>\n",
       "      <td>60.000</td>\n",
       "      <td>9550</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Corner</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.000</td>\n",
       "      <td>2473</td>\n",
       "      <td>1717</td>\n",
       "      <td>307</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>84.000</td>\n",
       "      <td>14260</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.500</td>\n",
       "      <td>3343</td>\n",
       "      <td>2198</td>\n",
       "      <td>84</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 114 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  MSSubClass MSZoning  LotFrontage  LotArea  Street  Alley  LotShape  \\\n",
       "0       SC60       RL       65.000     8450       2      0         4   \n",
       "1       SC20       RL       80.000     9600       2      0         4   \n",
       "2       SC60       RL       68.000    11250       2      0         3   \n",
       "3       SC70       RL       60.000     9550       2      0         3   \n",
       "4       SC60       RL       84.000    14260       2      0         3   \n",
       "\n",
       "  LandContour  Utilities LotConfig      ...        SimplPoolScore  \\\n",
       "0         Lvl          4    Inside      ...                   NaN   \n",
       "1         Lvl          4       FR2      ...                   NaN   \n",
       "2         Lvl          4    Inside      ...                   NaN   \n",
       "3         Lvl          4    Corner      ...                   NaN   \n",
       "4         Lvl          4       FR2      ...                   NaN   \n",
       "\n",
       "  SimplGarageScore SimplFireplaceScore SimplKitchenScore TotalBath AllSF  \\\n",
       "0              NaN                 NaN               NaN     3.500  2566   \n",
       "1              NaN                 NaN               NaN     2.500  2524   \n",
       "2              NaN                 NaN               NaN     3.500  2706   \n",
       "3              NaN                 NaN               NaN     2.000  2473   \n",
       "4              NaN                 NaN               NaN     3.500  3343   \n",
       "\n",
       "   AllFlrsSF  AllPorchSF  HasMasVnr  BoughtOffPlan  \n",
       "0       1710          61          1              0  \n",
       "1       1262           0          0              0  \n",
       "2       1786          42          1              0  \n",
       "3       1717         307          0              0  \n",
       "4       2198          84          1              0  \n",
       "\n",
       "[5 rows x 114 columns]"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Find most important features relative to target\n",
      "0.616027698277\n"
     ]
    }
   ],
   "source": [
    "# Find most important features relative to target\n",
    "print(\"Find most important features relative to target\")\n",
    "corr = train.corr()\n",
    "corr.sort_values([\"SalePrice\"], ascending = False, inplace = True)\n",
    "#print(corr.SalePrice)\n",
    "\n",
    "threshold = corr.SalePrice.iloc[11]  #the first one is SalePrice itself,from 1-11\n",
    "print(threshold)\n",
    "top10_cols = (corr.SalePrice[corr['SalePrice']>threshold]).axes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create new features\n",
    "# 3* Polynomials on the top 10 existing features\n",
    "def Polynomials_top10(df, top10_cols):\n",
    "    for i in range(1,11):\n",
    "        new_cols_2 = top10_cols[0][i] + '_s' + str(2)\n",
    "        new_cols_3 = top10_cols[0][i] + '_s' + str(3)\n",
    "        new_cols_sq = top10_cols[0][i] + '_sq'\n",
    "        \n",
    "        df[new_cols_2] = df[top10_cols[0][i]] ** 2\n",
    "        df[new_cols_3] = df[top10_cols[0][i]] ** 3\n",
    "        df[new_cols_sq] = np.sqrt(df[top10_cols[0][i]]) \n",
    "        \n",
    "    return df\n",
    "\n",
    "\n",
    "train = Polynomials_top10(train, top10_cols)\n",
    "test = Polynomials_top10(test,top10_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>MSZoning</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>LandContour</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LotConfig</th>\n",
       "      <th>...</th>\n",
       "      <th>GarageArea_sq</th>\n",
       "      <th>TotalBath_s2</th>\n",
       "      <th>TotalBath_s3</th>\n",
       "      <th>TotalBath_sq</th>\n",
       "      <th>ExterQual_s2</th>\n",
       "      <th>ExterQual_s3</th>\n",
       "      <th>ExterQual_sq</th>\n",
       "      <th>1stFlrSF_s2</th>\n",
       "      <th>1stFlrSF_s3</th>\n",
       "      <th>1stFlrSF_sq</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>65.000</td>\n",
       "      <td>8450</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>23.409</td>\n",
       "      <td>12.250</td>\n",
       "      <td>42.875</td>\n",
       "      <td>1.871</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>1.414</td>\n",
       "      <td>732736</td>\n",
       "      <td>627222016</td>\n",
       "      <td>29.257</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SC20</td>\n",
       "      <td>RL</td>\n",
       "      <td>80.000</td>\n",
       "      <td>9600</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>21.448</td>\n",
       "      <td>6.250</td>\n",
       "      <td>15.625</td>\n",
       "      <td>1.581</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1.000</td>\n",
       "      <td>1592644</td>\n",
       "      <td>2009916728</td>\n",
       "      <td>35.525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>68.000</td>\n",
       "      <td>11250</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>24.658</td>\n",
       "      <td>12.250</td>\n",
       "      <td>42.875</td>\n",
       "      <td>1.871</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>1.414</td>\n",
       "      <td>846400</td>\n",
       "      <td>778688000</td>\n",
       "      <td>30.332</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SC70</td>\n",
       "      <td>RL</td>\n",
       "      <td>60.000</td>\n",
       "      <td>9550</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>Corner</td>\n",
       "      <td>...</td>\n",
       "      <td>25.338</td>\n",
       "      <td>4.000</td>\n",
       "      <td>8.000</td>\n",
       "      <td>1.414</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1.000</td>\n",
       "      <td>923521</td>\n",
       "      <td>887503681</td>\n",
       "      <td>31.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SC60</td>\n",
       "      <td>RL</td>\n",
       "      <td>84.000</td>\n",
       "      <td>14260</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>4</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>28.914</td>\n",
       "      <td>12.250</td>\n",
       "      <td>42.875</td>\n",
       "      <td>1.871</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>1.414</td>\n",
       "      <td>1311025</td>\n",
       "      <td>1501123625</td>\n",
       "      <td>33.838</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 144 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  MSSubClass MSZoning  LotFrontage  LotArea  Street  Alley  LotShape  \\\n",
       "0       SC60       RL       65.000     8450       2      0         4   \n",
       "1       SC20       RL       80.000     9600       2      0         4   \n",
       "2       SC60       RL       68.000    11250       2      0         3   \n",
       "3       SC70       RL       60.000     9550       2      0         3   \n",
       "4       SC60       RL       84.000    14260       2      0         3   \n",
       "\n",
       "  LandContour  Utilities LotConfig     ...       GarageArea_sq TotalBath_s2  \\\n",
       "0         Lvl          4    Inside     ...              23.409       12.250   \n",
       "1         Lvl          4       FR2     ...              21.448        6.250   \n",
       "2         Lvl          4    Inside     ...              24.658       12.250   \n",
       "3         Lvl          4    Corner     ...              25.338        4.000   \n",
       "4         Lvl          4       FR2     ...              28.914       12.250   \n",
       "\n",
       "  TotalBath_s3 TotalBath_sq ExterQual_s2 ExterQual_s3  ExterQual_sq  \\\n",
       "0       42.875        1.871            4            8         1.414   \n",
       "1       15.625        1.581            1            1         1.000   \n",
       "2       42.875        1.871            4            8         1.414   \n",
       "3        8.000        1.414            1            1         1.000   \n",
       "4       42.875        1.871            4            8         1.414   \n",
       "\n",
       "   1stFlrSF_s2  1stFlrSF_s3  1stFlrSF_sq  \n",
       "0       732736    627222016       29.257  \n",
       "1      1592644   2009916728       35.525  \n",
       "2       846400    778688000       30.332  \n",
       "3       923521    887503681       31.000  \n",
       "4      1311025   1501123625       33.838  \n",
       "\n",
       "[5 rows x 144 columns]"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numerical features : 97\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 1456 entries, 0 to 1459\n",
      "Columns: 144 entries, MSSubClass to 1stFlrSF_sq\n",
      "dtypes: float64(16), int64(82), object(46)\n",
      "memory usage: 1.6+ MB\n",
      "NAs for numerical features in df : 81\n",
      "Remaining NAs for numerical features in df : 0\n"
     ]
    }
   ],
   "source": [
    "#对训练集的其他数值型特征进行空缺值填补（中值填补）\n",
    "#返回填补后的dataframe，以及每列的中值，用于填补测试集的空缺值\n",
    "# 数值型特征还要进行数据标准化\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "def fillna_numerical_train(df):\n",
    "    # 除了 object 类型 以外的数据列\n",
    "    numerical_features = df.select_dtypes(exclude = [\"object\"]).columns\n",
    "    # 删除 SalePrice 标签列\n",
    "    numerical_features = numerical_features.drop(\"SalePrice\")\n",
    "    print(\"Numerical features : \" + str(len(numerical_features)))\n",
    "\n",
    "    df.info()\n",
    "    df_num = df[numerical_features]\n",
    "    #df_num.info()\n",
    "    \n",
    "    medians = df_num.median() \n",
    "    # Handle remaining missing values for numerical features by using median as replacement\n",
    "    print(\"NAs for numerical features in df : \" + str(df_num.isnull().values.sum()))\n",
    "    df_num = df_num.fillna(medians)\n",
    "    print(\"Remaining NAs for numerical features in df : \" + str(df_num.isnull().values.sum()))\n",
    "\n",
    "    #df_num.info()\n",
    "    # 分别初始化对特征和目标值的标准化器\n",
    "    ss_X = StandardScaler()\n",
    "\n",
    "    # 对训练特征进行标准化处理\n",
    "    temp = ss_X.fit_transform(df_num)\n",
    "    df_num = pd.DataFrame(data=temp, columns=numerical_features, index =df_num.index)\n",
    "    \n",
    "    return df_num, medians, ss_X\n",
    "\n",
    "train_num, medians, ss_X = fillna_numerical_train(train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LandSlope</th>\n",
       "      <th>OverallQual</th>\n",
       "      <th>OverallCond</th>\n",
       "      <th>YearBuilt</th>\n",
       "      <th>...</th>\n",
       "      <th>GarageArea_sq</th>\n",
       "      <th>TotalBath_s2</th>\n",
       "      <th>TotalBath_s3</th>\n",
       "      <th>TotalBath_sq</th>\n",
       "      <th>ExterQual_s2</th>\n",
       "      <th>ExterQual_s3</th>\n",
       "      <th>ExterQual_sq</th>\n",
       "      <th>1stFlrSF_s2</th>\n",
       "      <th>1stFlrSF_s3</th>\n",
       "      <th>1stFlrSF_sq</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.227</td>\n",
       "      <td>-0.203</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>0.701</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>1.253</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>1.054</td>\n",
       "      <td>...</td>\n",
       "      <td>0.415</td>\n",
       "      <td>1.853</td>\n",
       "      <td>1.869</td>\n",
       "      <td>1.525</td>\n",
       "      <td>1.310</td>\n",
       "      <td>1.310</td>\n",
       "      <td>1.310</td>\n",
       "      <td>-0.747</td>\n",
       "      <td>-0.624</td>\n",
       "      <td>-0.822</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.670</td>\n",
       "      <td>-0.086</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>0.701</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>-0.694</td>\n",
       "      <td>1.858</td>\n",
       "      <td>0.159</td>\n",
       "      <td>...</td>\n",
       "      <td>0.109</td>\n",
       "      <td>0.214</td>\n",
       "      <td>0.053</td>\n",
       "      <td>0.449</td>\n",
       "      <td>-0.763</td>\n",
       "      <td>-0.763</td>\n",
       "      <td>-0.763</td>\n",
       "      <td>0.118</td>\n",
       "      <td>-0.024</td>\n",
       "      <td>0.363</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.316</td>\n",
       "      <td>0.081</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>-1.029</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>1.253</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>0.988</td>\n",
       "      <td>...</td>\n",
       "      <td>0.611</td>\n",
       "      <td>1.853</td>\n",
       "      <td>1.869</td>\n",
       "      <td>1.525</td>\n",
       "      <td>1.310</td>\n",
       "      <td>1.310</td>\n",
       "      <td>1.310</td>\n",
       "      <td>-0.632</td>\n",
       "      <td>-0.559</td>\n",
       "      <td>-0.619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.080</td>\n",
       "      <td>-0.091</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>-1.029</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>1.253</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>-1.861</td>\n",
       "      <td>...</td>\n",
       "      <td>0.717</td>\n",
       "      <td>-0.400</td>\n",
       "      <td>-0.455</td>\n",
       "      <td>-0.171</td>\n",
       "      <td>-0.763</td>\n",
       "      <td>-0.763</td>\n",
       "      <td>-0.763</td>\n",
       "      <td>-0.555</td>\n",
       "      <td>-0.511</td>\n",
       "      <td>-0.492</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.789</td>\n",
       "      <td>0.387</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>-1.029</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>1.253</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>0.954</td>\n",
       "      <td>...</td>\n",
       "      <td>1.276</td>\n",
       "      <td>1.853</td>\n",
       "      <td>1.869</td>\n",
       "      <td>1.525</td>\n",
       "      <td>1.310</td>\n",
       "      <td>1.310</td>\n",
       "      <td>1.310</td>\n",
       "      <td>-0.165</td>\n",
       "      <td>-0.245</td>\n",
       "      <td>0.044</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 97 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   LotFrontage  LotArea  Street  Alley  LotShape  Utilities  LandSlope  \\\n",
       "0        0.227   -0.203   0.064 -0.243     0.701      0.026      0.226   \n",
       "1        0.670   -0.086   0.064 -0.243     0.701      0.026      0.226   \n",
       "2        0.316    0.081   0.064 -0.243    -1.029      0.026      0.226   \n",
       "3        0.080   -0.091   0.064 -0.243    -1.029      0.026      0.226   \n",
       "4        0.789    0.387   0.064 -0.243    -1.029      0.026      0.226   \n",
       "\n",
       "   OverallQual  OverallCond  YearBuilt     ...       GarageArea_sq  \\\n",
       "0        1.253       -0.419      1.054     ...               0.415   \n",
       "1       -0.694        1.858      0.159     ...               0.109   \n",
       "2        1.253       -0.419      0.988     ...               0.611   \n",
       "3        1.253       -0.419     -1.861     ...               0.717   \n",
       "4        1.253       -0.419      0.954     ...               1.276   \n",
       "\n",
       "   TotalBath_s2  TotalBath_s3  TotalBath_sq  ExterQual_s2  ExterQual_s3  \\\n",
       "0         1.853         1.869         1.525         1.310         1.310   \n",
       "1         0.214         0.053         0.449        -0.763        -0.763   \n",
       "2         1.853         1.869         1.525         1.310         1.310   \n",
       "3        -0.400        -0.455        -0.171        -0.763        -0.763   \n",
       "4         1.853         1.869         1.525         1.310         1.310   \n",
       "\n",
       "   ExterQual_sq  1stFlrSF_s2  1stFlrSF_s3  1stFlrSF_sq  \n",
       "0         1.310       -0.747       -0.624       -0.822  \n",
       "1        -0.763        0.118       -0.024        0.363  \n",
       "2         1.310       -0.632       -0.559       -0.619  \n",
       "3        -0.763       -0.555       -0.511       -0.492  \n",
       "4         1.310       -0.165       -0.245        0.044  \n",
       "\n",
       "[5 rows x 97 columns]"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_num.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numerical features : 97\n",
      "NAs for numerical features in df : 88\n",
      "Remaining NAs for numerical features in df : 0\n"
     ]
    }
   ],
   "source": [
    "#对测试集的其他数值型特征进行空缺值填补（用训练集中相应列的中值填补）\n",
    "def fillna_numerical_test(df, medians, ss_X):\n",
    "    numerical_features = df.select_dtypes(exclude = [\"object\"]).columns\n",
    "    #numerical_features = numerical_features.drop(\"SalePrice\")  #测试集中没有SalePrice\n",
    "    print(\"Numerical features : \" + str(len(numerical_features)))\n",
    "\n",
    "    df_num = df[numerical_features]\n",
    "    \n",
    "    # Handle remaining missing values for numerical features by using median as replacement\n",
    "    print(\"NAs for numerical features in df : \" + str(df_num.isnull().values.sum()))\n",
    "    df_num = df_num.fillna(medians)\n",
    "    print(\"Remaining NAs for numerical features in df : \" + str(df_num.isnull().values.sum()))\n",
    "\n",
    "    #对数值特征进行标准化\n",
    "    temp = ss_X.transform(df_num)\n",
    "    df_num = pd.DataFrame(data=temp, columns=numerical_features, index =df_num.index )\n",
    "    return df_num\n",
    "\n",
    "test_num = fillna_numerical_test(test, medians, ss_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Categorical features : 46\n",
      "NAs for categorical features in df : 61223\n",
      "Remaining NAs for categorical features in df : 0\n"
     ]
    }
   ],
   "source": [
    "def get_dummies_cat(df):\n",
    "    categorical_features = df.select_dtypes(include = [\"object\"]).columns\n",
    "    print(\"Categorical features : \" + str(len(categorical_features)))\n",
    "    df_cat = df[categorical_features]\n",
    "    \n",
    "\n",
    "    # Create dummy features for categorical values via one-hot encoding\n",
    "    print(\"NAs for categorical features in df : \" + str(df_cat.isnull().values.sum()))\n",
    "    df_cat = pd.get_dummies(df_cat,dummy_na=True)\n",
    "    print(\"Remaining NAs for categorical features in df : \" + str(df_cat.isnull().values.sum()))\n",
    "    \n",
    "    return df_cat\n",
    "\n",
    "#必须考虑类别型特征的取值范围（训练集和测试的取值范围可能不同）\n",
    "#train_cat = get_dummies_cat(train)\n",
    "#test_cat = get_dummies_cat(test)\n",
    "\n",
    "n_train_samples = train.shape[0]  \n",
    "train_test = pd.concat((train, test), axis=0)\n",
    "train_test_cat = get_dummies_cat(train_test)\n",
    "   \n",
    "train_cat = train_test_cat.iloc[:n_train_samples, :]\n",
    "test_cat = train_test_cat.iloc[n_train_samples:, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>BldgType_1Fam</th>\n",
       "      <th>BldgType_2fmCon</th>\n",
       "      <th>BldgType_Duplex</th>\n",
       "      <th>BldgType_Twnhs</th>\n",
       "      <th>BldgType_TwnhsE</th>\n",
       "      <th>BldgType_nan</th>\n",
       "      <th>CentralAir_N</th>\n",
       "      <th>CentralAir_Y</th>\n",
       "      <th>CentralAir_nan</th>\n",
       "      <th>Condition1_Artery</th>\n",
       "      <th>...</th>\n",
       "      <th>SimplGarageQual_nan</th>\n",
       "      <th>SimplGarageScore_nan</th>\n",
       "      <th>SimplHeatingQC_nan</th>\n",
       "      <th>SimplKitchenQual_nan</th>\n",
       "      <th>SimplKitchenScore_nan</th>\n",
       "      <th>SimplOverallCond_nan</th>\n",
       "      <th>SimplOverallGrade_nan</th>\n",
       "      <th>SimplOverallQual_nan</th>\n",
       "      <th>SimplPoolQC_nan</th>\n",
       "      <th>SimplPoolScore_nan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 246 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   BldgType_1Fam  BldgType_2fmCon  BldgType_Duplex  BldgType_Twnhs  \\\n",
       "0              1                0                0               0   \n",
       "1              1                0                0               0   \n",
       "2              1                0                0               0   \n",
       "3              1                0                0               0   \n",
       "4              1                0                0               0   \n",
       "\n",
       "   BldgType_TwnhsE  BldgType_nan  CentralAir_N  CentralAir_Y  CentralAir_nan  \\\n",
       "0                0             0             0             1               0   \n",
       "1                0             0             0             1               0   \n",
       "2                0             0             0             1               0   \n",
       "3                0             0             0             1               0   \n",
       "4                0             0             0             1               0   \n",
       "\n",
       "   Condition1_Artery         ...          SimplGarageQual_nan  \\\n",
       "0                  0         ...                            1   \n",
       "1                  0         ...                            1   \n",
       "2                  0         ...                            1   \n",
       "3                  0         ...                            1   \n",
       "4                  0         ...                            1   \n",
       "\n",
       "   SimplGarageScore_nan  SimplHeatingQC_nan  SimplKitchenQual_nan  \\\n",
       "0                     1                   1                     1   \n",
       "1                     1                   1                     1   \n",
       "2                     1                   1                     1   \n",
       "3                     1                   1                     1   \n",
       "4                     1                   1                     1   \n",
       "\n",
       "   SimplKitchenScore_nan  SimplOverallCond_nan  SimplOverallGrade_nan  \\\n",
       "0                      1                     1                      1   \n",
       "1                      1                     1                      1   \n",
       "2                      1                     1                      1   \n",
       "3                      1                     1                      1   \n",
       "4                      1                     1                      1   \n",
       "\n",
       "   SimplOverallQual_nan  SimplPoolQC_nan  SimplPoolScore_nan  \n",
       "0                     1                1                   1  \n",
       "1                     1                1                   1  \n",
       "2                     1                1                   1  \n",
       "3                     1                1                   1  \n",
       "4                     1                1                   1  \n",
       "\n",
       "[5 rows x 246 columns]"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_cat.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "New number of features : 343\n",
      "New number of features : 343\n"
     ]
    }
   ],
   "source": [
    "# Join categorical and numerical features\n",
    "def joint_num_cat(df_num, df_cat):\n",
    "    df = pd.concat([df_num, df_cat], axis = 1, ignore_index=False)\n",
    "    print(\"New number of features : \" + str(df.shape[1]))\n",
    "    \n",
    "    return df\n",
    "\n",
    "FE_train = joint_num_cat(train_num, train_cat)\n",
    "FE_test = joint_num_cat(test_num, test_cat)\n",
    "\n",
    "FE_train = pd.concat([FE_train, train['SalePrice']], axis = 1)\n",
    "FE_test = pd.concat([test_id,FE_test], axis = 1)\n",
    "\n",
    "FE_train.to_csv('AmesHouse_FE_train.csv', index=True)\n",
    "FE_test.to_csv('AmesHouse_FE_test.csv', index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LandSlope</th>\n",
       "      <th>OverallQual</th>\n",
       "      <th>OverallCond</th>\n",
       "      <th>YearBuilt</th>\n",
       "      <th>...</th>\n",
       "      <th>SimplGarageScore_nan</th>\n",
       "      <th>SimplHeatingQC_nan</th>\n",
       "      <th>SimplKitchenQual_nan</th>\n",
       "      <th>SimplKitchenScore_nan</th>\n",
       "      <th>SimplOverallCond_nan</th>\n",
       "      <th>SimplOverallGrade_nan</th>\n",
       "      <th>SimplOverallQual_nan</th>\n",
       "      <th>SimplPoolQC_nan</th>\n",
       "      <th>SimplPoolScore_nan</th>\n",
       "      <th>SalePrice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.227</td>\n",
       "      <td>-0.203</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>0.701</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>1.253</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>1.054</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>208500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.670</td>\n",
       "      <td>-0.086</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>0.701</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>-0.694</td>\n",
       "      <td>1.858</td>\n",
       "      <td>0.159</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>181500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.316</td>\n",
       "      <td>0.081</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>-1.029</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>1.253</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>0.988</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>223500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.080</td>\n",
       "      <td>-0.091</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>-1.029</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>1.253</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>-1.861</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>140000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.789</td>\n",
       "      <td>0.387</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>-1.029</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>1.253</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>0.954</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>250000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 344 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   LotFrontage  LotArea  Street  Alley  LotShape  Utilities  LandSlope  \\\n",
       "0        0.227   -0.203   0.064 -0.243     0.701      0.026      0.226   \n",
       "1        0.670   -0.086   0.064 -0.243     0.701      0.026      0.226   \n",
       "2        0.316    0.081   0.064 -0.243    -1.029      0.026      0.226   \n",
       "3        0.080   -0.091   0.064 -0.243    -1.029      0.026      0.226   \n",
       "4        0.789    0.387   0.064 -0.243    -1.029      0.026      0.226   \n",
       "\n",
       "   OverallQual  OverallCond  YearBuilt    ...      SimplGarageScore_nan  \\\n",
       "0        1.253       -0.419      1.054    ...                         1   \n",
       "1       -0.694        1.858      0.159    ...                         1   \n",
       "2        1.253       -0.419      0.988    ...                         1   \n",
       "3        1.253       -0.419     -1.861    ...                         1   \n",
       "4        1.253       -0.419      0.954    ...                         1   \n",
       "\n",
       "   SimplHeatingQC_nan  SimplKitchenQual_nan  SimplKitchenScore_nan  \\\n",
       "0                   1                     1                      1   \n",
       "1                   1                     1                      1   \n",
       "2                   1                     1                      1   \n",
       "3                   1                     1                      1   \n",
       "4                   1                     1                      1   \n",
       "\n",
       "   SimplOverallCond_nan  SimplOverallGrade_nan  SimplOverallQual_nan  \\\n",
       "0                     1                      1                     1   \n",
       "1                     1                      1                     1   \n",
       "2                     1                      1                     1   \n",
       "3                     1                      1                     1   \n",
       "4                     1                      1                     1   \n",
       "\n",
       "   SimplPoolQC_nan  SimplPoolScore_nan  SalePrice  \n",
       "0                1                   1     208500  \n",
       "1                1                   1     181500  \n",
       "2                1                   1     223500  \n",
       "3                1                   1     140000  \n",
       "4                1                   1     250000  \n",
       "\n",
       "[5 rows x 344 columns]"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FE_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Id</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LandSlope</th>\n",
       "      <th>OverallQual</th>\n",
       "      <th>OverallCond</th>\n",
       "      <th>...</th>\n",
       "      <th>SimplGarageQual_nan</th>\n",
       "      <th>SimplGarageScore_nan</th>\n",
       "      <th>SimplHeatingQC_nan</th>\n",
       "      <th>SimplKitchenQual_nan</th>\n",
       "      <th>SimplKitchenScore_nan</th>\n",
       "      <th>SimplOverallCond_nan</th>\n",
       "      <th>SimplOverallGrade_nan</th>\n",
       "      <th>SimplOverallQual_nan</th>\n",
       "      <th>SimplPoolQC_nan</th>\n",
       "      <th>SimplPoolScore_nan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1461</td>\n",
       "      <td>0.670</td>\n",
       "      <td>0.119</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>0.701</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>-0.694</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1462</td>\n",
       "      <td>0.700</td>\n",
       "      <td>0.387</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>-1.029</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>-0.694</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1463</td>\n",
       "      <td>0.493</td>\n",
       "      <td>0.343</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>-1.029</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>-0.694</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1464</td>\n",
       "      <td>0.611</td>\n",
       "      <td>-0.048</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>-1.029</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>-0.694</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1465</td>\n",
       "      <td>-0.422</td>\n",
       "      <td>-0.552</td>\n",
       "      <td>0.064</td>\n",
       "      <td>-0.243</td>\n",
       "      <td>-1.029</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.226</td>\n",
       "      <td>1.253</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 344 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Id  LotFrontage  LotArea  Street  Alley  LotShape  Utilities  LandSlope  \\\n",
       "0  1461        0.670    0.119   0.064 -0.243     0.701      0.026      0.226   \n",
       "1  1462        0.700    0.387   0.064 -0.243    -1.029      0.026      0.226   \n",
       "2  1463        0.493    0.343   0.064 -0.243    -1.029      0.026      0.226   \n",
       "3  1464        0.611   -0.048   0.064 -0.243    -1.029      0.026      0.226   \n",
       "4  1465       -0.422   -0.552   0.064 -0.243    -1.029      0.026      0.226   \n",
       "\n",
       "   OverallQual  OverallCond         ...          SimplGarageQual_nan  \\\n",
       "0       -0.694       -0.419         ...                            1   \n",
       "1       -0.694       -0.419         ...                            1   \n",
       "2       -0.694       -0.419         ...                            1   \n",
       "3       -0.694       -0.419         ...                            1   \n",
       "4        1.253       -0.419         ...                            1   \n",
       "\n",
       "   SimplGarageScore_nan  SimplHeatingQC_nan  SimplKitchenQual_nan  \\\n",
       "0                     1                   1                     1   \n",
       "1                     1                   1                     1   \n",
       "2                     1                   1                     1   \n",
       "3                     1                   1                     1   \n",
       "4                     1                   1                     1   \n",
       "\n",
       "   SimplKitchenScore_nan  SimplOverallCond_nan  SimplOverallGrade_nan  \\\n",
       "0                      1                     1                      1   \n",
       "1                      1                     1                      1   \n",
       "2                      1                     1                      1   \n",
       "3                      1                     1                      1   \n",
       "4                      1                     1                      1   \n",
       "\n",
       "   SimplOverallQual_nan  SimplPoolQC_nan  SimplPoolScore_nan  \n",
       "0                     1                1                   1  \n",
       "1                     1                1                   1  \n",
       "2                     1                1                   1  \n",
       "3                     1                1                   1  \n",
       "4                     1                1                   1  \n",
       "\n",
       "[5 rows x 344 columns]"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FE_test.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
